package cn.sytton.taffe.framework.factory;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.io.File;

/**
 * 文件处理工厂类
 *
 * @author skyrock
 */
@Component
public class FileUtil {

    private static final String DEFAULT_PATH = "files";

    @Value("${file.path:}")
    private String rootPath;

    public String getRootPath(String rootPath, String defaultPath) {
        if (StringUtils.hasText(rootPath)) {
            File file = new File(rootPath);
            if (!file.exists()) {
                if (file.mkdirs()) {
                    return rootPath;
                }
            }
            return "";
        } else {
            String path = System.getProperty("user.dir") + File.separator + defaultPath;
            File file = new File(path);
            if (!file.exists()) {
                if (file.mkdirs()) {
                    return path;
                }
            }
            return "";
        }
    }

    /**
     * 获取文件的根目录
     * @return 跟路径
     */
    public String getFileRootPath() {
        return getRootPath(rootPath, DEFAULT_PATH);
    }
}
